package com.dyp.global_exception_handler.ctr;



import com.dyp.global_exception_handler.model.Book;
import com.dyp.global_exception_handler.model.CustomHttpStatusCodeException;
import com.dyp.global_exception_handler.model.People;
import com.dyp.global_exception_handler.model.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

//使用RestController可以省略在方法上的@ResponseBody注解
@RestController
@RequestMapping(value = "/mvc")
@Tag(name = "api for mvc")
public class RestfulCtr {



    //    @RequestMapping(value = "people/info/{id}", method = RequestMethod.POST)
    @PostMapping(value = "people/info/{id}")
    public People sayHi4(@PathVariable(name = "id") int id,
                         @RequestParam int num,
                         @RequestBody People people) {
        if(true)
        {
            throw new CustomHttpStatusCodeException("test global exception handler");
        }
        people.setId(id);
        people.setNum(num);
        return people;
    }

    @GetMapping("/global/data/binding/{id}")
    @Operation(summary = "用来测试全局数据绑定", method = "GET")
    public String hello(@PathVariable(name = "id") int id, Model model) {
        System.out.println(id);
        Map<String, Object> map = model.asMap();
        return map.toString();
    }

    @GetMapping("/global/data/process")
    @Operation(summary = "用来测试全局数据处理", method = "GET")
    public String hello1(Date date) {
        return date.toString();
    }

    //测试Date自动转换的时候把GlobalDataProcessing的@ControllerAdvice注释掉
    @PostMapping("/book")
    public Book createBook(@RequestBody Book book){
        System.out.println(book.toString());
        return book;
    }

    @GetMapping("/user/profile")
    public String showUserProfile(
            @ModelAttribute("currentUser") User user,  // 自动注入Model
            Model model) {
        // 方法内无需手动添加，user对象已存在于model中
        System.out.println(model.getAttribute("currentUser"));
        return "user-profile";  // 视图层可通过${currentUser}访问
    }
}
